package com.keqing.tomato.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.keqing.tomato.R;
import com.keqing.tomato.adapter.HistoryItemAdapter;
import com.keqing.tomato.bean.GroupInfo;
import com.keqing.tomato.bean.HistoryInfo;
import com.keqing.tomato.bean.Item;
import com.keqing.tomato.bean.ItemInfo;
import com.keqing.tomato.database.TomatoHistory;
import com.keqing.tomato.util.TimeUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class FragmentHistory extends Fragment {
    private List<Item> al_items = new ArrayList<>();
    private RecyclerView recycler_view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("tomato", "FragmentHistory - onCreateView()");
        View view = inflater.inflate(R.layout.fragment_history, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.e("tomato", "FragmentHistory - onViewCreated()");
//        recycler_view.addItemDecoration(new ItemAdapter.LineDecoration());

        recycler_view = view.findViewById(R.id.recycler_view);
        loadData();
    }

    private void loadData() {
//        List<ItemInfo> list = new ArrayList<>();
//        list.add(new ItemInfo(ItemInfo.type_group, new GroupInfo("8月25日", 60, 8)));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//
//        list.add(new ItemInfo(ItemInfo.type_group, new GroupInfo("8月24日", 60, 8)));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//
//        list.add(new ItemInfo(ItemInfo.type_group, new GroupInfo("8月23日", 60, 8)));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));
//        list.add(new ItemInfo(ItemInfo.type_child, new HistoryInfo("第一个番茄钟", 25, "12:09", "12:39")));


//        List<TomatoHistory> histories = new ArrayList<>();
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 25), new Date(2021, 7, 25)));
//
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 24), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 24), new Date(2021, 7, 25)));
//
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 23), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 23), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 23), new Date(2021, 7, 25)));
//
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));
//        histories.add(new TomatoHistory("未名番茄钟", 25, new Date(2021, 7, 22), new Date(2021, 7, 25)));

        List<TomatoHistory> histories = LitePal.select("id", "duration", "title", "startTime", "endTime")
                .order("startTime desc")
                .find(TomatoHistory.class);

        List<GroupInfo> groupInfoList = new ArrayList<>();
        int i = 0;
        while (i < histories.size() - 1) {
            Date firstDate = histories.get(i).getStartTime();
            int durationSum = 0;
            int historyCount = 0;
            for (int j = i; j < histories.size(); j++) {
                Date startTime = histories.get(j).getStartTime();
                if (TimeUtil.compare_same_date(startTime, firstDate)) {
                    durationSum += histories.get(j).getDuration();
                    historyCount++;
                } else {
                    groupInfoList.add(new GroupInfo(firstDate, durationSum, historyCount));
                    i = j;
                    break;
                }
                if (j == histories.size() - 1) {
                    groupInfoList.add(new GroupInfo(firstDate, durationSum, historyCount));
                    i = j;
                    break;
                }
            }
        }
        List<ItemInfo> list = new ArrayList<>();
        int firstDateIndex = 0;
        for (GroupInfo groupInfo : groupInfoList) {
            list.add(new ItemInfo(ItemInfo.type_group, new GroupInfo(groupInfo.getDateStr(), groupInfo.getDurationSum(), groupInfo.getHistoryCount())));
            for (int j = firstDateIndex; j < histories.size(); j++) {
                TomatoHistory tomato = histories.get(j);
                Date startTime = tomato.getStartTime();
                if (TimeUtil.compare_same_date(startTime, groupInfo.getDate())) {
                    list.add(new ItemInfo(ItemInfo.type_child,
                            new HistoryInfo(tomato.getTitle(), tomato.getDuration(),
                                    TimeUtil.getStringByFormat(tomato.getStartTime(), TimeUtil.dateFormatHM),
                                    TimeUtil.getStringByFormat(tomato.getEndTime(), TimeUtil.dateFormatHM)
                            )));
                } else {
                    firstDateIndex = j;
                    break;
                }
            }
        }

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        recycler_view.setLayoutManager(manager);
        HistoryItemAdapter adapter = new HistoryItemAdapter(list);
        recycler_view.setAdapter(adapter);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        loadData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("tomato", "FragmentHistory - onDestroy()");

    }
}